﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for ComicView.xaml
    /// </summary>
    public partial class ComicView
    {
        private readonly ComicController _comicControl;
        private readonly ComicStatusController _comicStatusController;
        private readonly OrderDetailController _orderDetailControl;

        private IList<Comic> _comics;
        private readonly IList<ComicStatus> _comicStatuses;
        private IList<ComicData> _comicDatas;

        public ComicView()
        {
            InitializeComponent();

            _comicControl = new ComicController();
            _comicStatusController = new ComicStatusController();
            _orderDetailControl = new OrderDetailController();

            _comics = _comicControl.GetAllComic();
            _comicStatuses = _comicStatusController.GetAllComicStatus();
            _comicDatas = _comicControl.GetAllComicData();
            dataGridComic.ItemsSource = _comicDatas;
        }

        private void RefreshGrid()
        {
            dataGridComic.ItemsSource = null;
            _comics = _comicControl.GetAllComic();
            _comicDatas = _comicControl.GetAllComicData();
            dataGridComic.ItemsSource = _comicDatas;
        }

        private void ButtonAddClick(object sender, RoutedEventArgs e)
        {
            var newWindow = new ComicEditorView();

            if (newWindow.ShowDialog() != false)
            {
            }
            else
            {
                RefreshGrid();
            }
        }

        private void ButtonDeleteClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridComic.SelectedItem == null)
                    throw new NullReferenceException();

                var comic = _comics
                        .Where(c => c.Id == _comics[dataGridComic.SelectedIndex].Id)
                        .Select(c => c)
                        .Single();

                _comicControl.DeleteComic(comic);
                MessageBox.Show("sukses menghapus comic");
                RefreshGrid();

                if (comic.Image != "defaultdefaultdefault.bmp")
                {
                    try
                    {
                        File.Delete(@"C:\xampp\htdocs\ComicQImage\" + comic.Image);
                    }
                        // ReSharper disable EmptyGeneralCatchClause
                    catch (Exception)
                        // ReSharper restore EmptyGeneralCatchClause
                    {
                    }
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih yang mau didelete");
            }
        }

        private void ButtonEditClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridComic.SelectedItem == null)
                    throw new NullReferenceException();

                var newWindow = new ComicEditorView(_comics[dataGridComic.SelectedIndex]);

                if (newWindow.ShowDialog() != false)
                {
                }
                else
                {
                    RefreshGrid();
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih yang mau diedit");
            }
        }

        private void ButtonRefreshClick(object sender, RoutedEventArgs e)
        {
            foreach (var comic in _comics)
            {
                var newComic = new Comic
                                   {
                                       Id = comic.Id,
                                       Title = comic.Title,
                                       Author = comic.Author,
                                       Publisher = comic.Publisher,
                                       Year = comic.Year,
                                       Rent = comic.Rent,
                                       ComicStatus = _comicStatuses[0],
                                       Image = comic.Image
                                   };

                _comicControl.EditComic(newComic);
            }

            var comicsNotAvailable = _orderDetailControl.GetAllNotAvailableComic();

            foreach (var comic in comicsNotAvailable)
            {
                var oldComic = _comicControl.GetComicById(comic.Id);

                var newComic = new Comic
                {
                    Id = oldComic.Id,
                    Title = oldComic.Title,
                    Author = oldComic.Author,
                    Publisher = oldComic.Publisher,
                    Year = oldComic.Year,
                    Rent = oldComic.Rent,
                    ComicStatus = _comicStatuses[1],
                    Image = oldComic.Image
                };

                _comicControl.EditComic(newComic);
            }

            var comicImages = _comics.Select(c => c.Image).ToList();
            var di = new DirectoryInfo(@"C:\xampp\htdocs\ComicQImage\");
            var files = di.GetFiles();

            foreach (var file in files)
            {
                if (!comicImages.Contains(file.Name) && !file.Name.Equals("defaultdefaultdefault.bmp"))
                {
                    try
                    {
                        File.Delete(@"C:\xampp\htdocs\ComicQImage\" + file.Name);
                    }
                    // ReSharper disable EmptyGeneralCatchClause
                    catch (Exception)
                    // ReSharper restore EmptyGeneralCatchClause
                    {
                    }
                }
            }

            MessageBox.Show("comic refreshed");
            RefreshGrid();
        }
    }
}
